﻿#region License, Terms and Conditions
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
#endregion
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using raya.Library;
using raya.Models.applications;
using raya.Models.people;

namespace raya.Controllers
{
    public class profileController : baseController
    {
        public ActionResult Index(string id)
        {
            if (string.IsNullOrEmpty(id) && Session["id"] == null)
            {
                return Redirect("/");
            }
            string pid = string.IsNullOrEmpty(id) ? Session["id"].ToString() : id;
            var data = GetProfile(pid);
            return View("Index", data);

        }

        public ActionResult Edit()
        {
            if (Session["id"] == null)
            {
                handleErrors("Your session has expired");
                return Redirect("/");
            }
            string message = "";
            people people = new people();
            if (Request.Form.Count > 0)
            {
                string errorMessage = "";
                if (Request.Files["profile_photo"] != null && !String.IsNullOrEmpty(Request.Files["profile_photo"].FileName))
                {
                    var file = Request.Files["profile_photo"];
                    if (file.ContentType.Substring(0, "image/".Length) != "image/")
                    {
                        errorMessage = "File selected is not an image";
                    }
                    else
                    {
                        string ext = file.FileName.Substring(file.FileName.IndexOf(".") + 1).ToLower();
                        if (Img.acceptedTypes.Contains(ext))
                        {
                            if (Img.saveImage(file, "/images/people/" + Session["id"] + "." + ext))
                            {
                                // thumbnail the image to 96x96 format (keeping the original)
                                string thumbnailUrl =
                                    Img.by_size("/images/people/" + Session["id"] +
                                                "." + ext, 96, 96, true);
                                people.SetProfilePhoto(Session["id"].ToString(), thumbnailUrl);
                            }
                        }
                        else
                        {
                            errorMessage = "Unsupported image type";
                        }
                    }

                    if (!string.IsNullOrEmpty(errorMessage))
                    {
                        handleErrors(errorMessage);
                        return View();
                    }
                }
                try
                {
#if AZURE
                    people.SavePerson(Session["id"].ToString(), Request.Form);
#else
                    people.SavePerson((int)Session["id"], Request.Form);
#endif
                    message = "Profile updated";
                }
                catch (Exception e)
                {
                    message = "Error saving profile (" + e.Message + ")";
                }
            }
            var data = GetProfile(Session["id"].ToString());
            data.message = message;

            people.Dispose();

            return View("Index", data);
        }

        public ActionResult Application(string pid, string aid, string mid)
        {
            if (Session["id"] == null)
            {
                handleErrors("Your session has expired");
                return Redirect("/");
            }
            var people = new people();
            var person = people.GetPerson(pid, true);
            var apps = new applications();
            ProfileApplication application = apps.get_person_application(pid, aid, mid);
            ApplicationCanvas data = new ApplicationCanvas
                                         {
                                             application = application,
                                             person = person,
                                             is_owner = Session["id"].ToString() == pid
                                         };
            people.Dispose();
            apps.Dispose();
            return View("application_canvas", data);
        }
    }
}